﻿@model PermissionDTO[]
@{
    ViewBag.Title = "新增角色";
}

<article class="page-container">
    <form class="form form-horizontal" id="form">
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>角色名称：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="" placeholder="" id="name" name="name">
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>拥有权限：</label>
            <div class="formControls col-xs-8 col-sm-9 check-box-list">
                <ul id="treePerm" class="ztree"></ul>
                <label id="perm-error" class="col-xs-4 error">未设置用户权限</label>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3">描述：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="" placeholder="" name="description">
            </div>
        </div>
        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <button type="submit" class="btn btn-primary radius" id="submit"><i class="Hui-iconfont">&#xe632;</i> 保存</button>
            </div>
        </div>
    </form>
</article>

@section HeaderStyle
{
    <!--zTree-->
    <link href="~/Areas/WebAdmin/FrontLib/zTree/v3/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" />
}

@section FooterScript
{
    <!--请在下方写此页面业务相关的脚本-->
    <script type="text/javascript" src="~/Areas/WebAdmin/FrontLib/jquery.validation/1.14.0/jquery.validate.js"></script>
    <script type="text/javascript" src="~/Areas/WebAdmin/FrontLib/jquery.validation/1.14.0/validate-methods.js"></script>
    <script type="text/javascript" src="~/Areas/WebAdmin/FrontLib/jquery.validation/1.14.0/messages_zh.js"></script>

    <!--zTree-->
    <script src="~/Areas/WebAdmin/FrontLib/zTree/v3/js/jquery.ztree.core-3.5.min.js"></script>
    <script src="~/Areas/WebAdmin/FrontLib/zTree/v3/js/jquery.ztree.excheck-3.5.min.js"></script>

    <script type="text/javascript">
        $(function () {
            var $permError = $("#perm-error");
            /*************** zTree 拥有权限 begin *******************/
            //获取拥有权限选中值
            var SelectPerms = function myfunction() {
                var treeObj = $.fn.zTree.getZTreeObj("treePerm");
                var nodes = treeObj.getCheckedNodes(true);
                var arr = new Array();
                //console.log(nodes.length);
                for (var i = 0; i < nodes.length; i++) {
                    arr.push(nodes[i].id);
                }

                return arr;
            }
            //点击权限单选框触发
            var treeOnCheck = function (e, treeId, treeNode) {
                var treeObj = $.fn.zTree.getZTreeObj("treePerm");
                var nodes = treeObj.getCheckedNodes(true);

                if (nodes.length <= 0) {
                    $permError.text("未设置用户权限").show();
                } else {
                    $permError.hide();
                }
            };
            //zTree 配置
            var setting = {
                view: {
                    showIcon: false
                },
                check: {
                    enable: true
                },
                data: {
                    simpleData: {
                        enable: true
                    }
                },
                callback: {
                    beforeCheck: true,
                    onCheck: treeOnCheck
                }

            };
            var nodes = [
                @{
                    var lastItem = Model.Last();
                    foreach (var item in Model)
                    {
                        //节点是否打开
                        string isOpen = (item.ParentId > 0).ToString().ToLower();
                        if (item!=lastItem)
                        {
                            @:{ id: @item.Id, pId: @item.ParentId, name: "@item.Name", open:@isOpen },
                        }
                        else
                        {
                            @:{ id: @item.Id, pId: @item.ParentId, name: "@item.Name", open: @isOpen }
                        }
                    }
                }
            ];

            //zTree 初始化
            $.fn.zTree.init($("#treePerm"), setting,nodes);
            /*************** zTree 拥有权限 end *******************/

            $("#form").validate({
                rules: {
                    name: {
                        required: true
                    }
                    //"permissions[]": {
                    //    required: true
                    //}
                },
                messages: {
                    //"permissions[]": {
                    //    required: '未设置用户权限'
                    //}
                },
                onkeyup: false,
                focusCleanup: true,
                success: "valid",
                submitHandler: function (form) {
                    //拥有权限验证
                    var arrPerms = SelectPerms();
                    if (arrPerms.length<=0) {
                        $permError.text("未设置用户权限").show();
                        return false;
                    } else {
                        $permError.hide();
                    }

                    //禁用保存按钮，防止重复提交
                    $("#submit").prop("disabled", true);

                    $(form).ajaxSubmit({
                        type: 'POST',
                        url: "/WebAdmin/Role/Add",
                        data: { permissions: arrPerms }, //拥有权限
                        success: function (res) {
                            if (res.status == "ok") {
                                parent.layer.msg('新增成功!', { icon: 1, time: 1000 }, function () {
                                    parent.$('.btn-refresh').click();
                                    //parent.location.reload(); //刷新父页面
                                    var index = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(index);
                                });
                            } else if (res.status == "redirect") {
                                location.href = res.data;
                            } else if (res.status == "error") {
                                layer.msg(res.errorMsg, { icon: 2, time: 1000 });
                                //解除禁用
                                $("#submit").prop("disabled", false);
                            } else {
                                layer.msg("新增失败", { icon: 2, time: 1000 });
                                //解除禁用
                                $("#submit").prop("disabled", false);
                            }
                        },
                        error: function (XmlHttpRequest, textStatus, errorThrown) {
                            layer.msg('网络出错!', { icon: 2, time: 1000 });
                            //解除禁用
                            $("#submit").prop("disabled", false);
                        }
                    });
                }
            });
        });
    </script>
    <!--/请在上方写此页面业务相关的脚本-->
}

